//
//  SelectCountryPage.swift
//  Uber
//
//  Created by chen on 16/3/12.
//  Copyright © 2016年 chen. All rights reserved.
//

import UIKit

class SelectCountryPage: FxBasePage,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet var tableView: UITableView!
    //存放国家信息
    var countrys:[CountryInfo]?
    //存放国家首字母的表头
    var headers:[String]?
    //分组存放表头指定下的国家数组,字典
    var dictDatas:[String:AnyObject]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "选择国家/地区"
        setNavigationItem("取消", selector: "doBack", isRight: false)
        initCountry()
        initDatas()
        //注册一个可以重用的单元格
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "CellID")
        self.tableView.reloadData()
        // Do any additional setup after loading the view.
    }
    func initCountry(){
        
        //获取当前用户的位置信息
        let locale = NSLocale.currentLocale()
        //获取所有国家的数组，可以参考iso设置中的”当前位置信息“
        let arrCodes = NSLocale.ISOCountryCodes()
        //初始化这个countrys数组
        self.countrys = []
        var info:CountryInfo!
        for code in arrCodes {
            info = CountryInfo()
            info.code = code
            info.name = locale.displayNameForKey(NSLocaleCountryCode, value: code)!
            self.countrys?.append(info)
        }
//        self.countrys?.sortInPlace({
//            (arg1:CountryInfo, arg2:CountryInfo) -> Bool in
//            return arg1.name.localizedCaseInsensitiveCompare(arg2.name) == .OrderedAscending
//        })
        
    }
    //对countrys数组中的数据进行分类
    func initDatas(){
        
        var firstLetter:String!
        var datas:[CountryInfo]!
        
        //初始化
        //字母头
        headers = []
        //该字母头下面的国家对象
        datas = []
        //字典存放的是字母头和国家对象数组
        dictDatas = [:]
        for country in countrys!{
            firstLetter = FxString.firstCharactor(country.name)
            if (headers?.contains(firstLetter) == false){
                if datas.count > 0{
                    dictDatas![(headers?.last)!] = datas
                }
                headers?.append(firstLetter)
                datas = []
            }
            datas.append(country)
        }
        dictDatas![(headers?.last)!] = datas
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return headers!.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identify = "CellID"
        let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath) as UITableViewCell
        let key = headers![indexPath.section]
        let datas = dictDatas![key] as! [CountryInfo]
        let country = datas[indexPath.row]
        cell.accessoryType = .None
        cell.textLabel?.text = country.name
        return cell
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let key = headers![section]
        let datas = dictDatas![key] as! [CountryInfo]
        return datas.count
    }
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return headers![section]
    }
//    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//        
//    }
}
